import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpStatus,
} from '@nestjs/common';
import { BizFailedException } from './biz-failed.exception';
import { BizFailedCodeEnum, bizFailedDesc } from "@/common/biz-failed/biz-failed.enum";

@Catch(BizFailedException)
export class BizFailedExceptionFilter implements ExceptionFilter {
  catch(bizFailedError: Error, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const bizFailedCode = bizFailedError.message as unknown as BizFailedCodeEnum;
    const body = {
      code: Number(bizFailedCode),
      message: bizFailedDesc[bizFailedCode],
      data: null,
    };
    response.status(HttpStatus.OK).json(body);
  }
}
